package org.jimm.app;

import org.jimm.app.XStatusAdapter;
import org.jimm.protocol.AccountsManager;
import org.jimm.service.Preferences;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author r.u.s.h.a.n.
 */
public class XStatusActivity extends Activity implements AdapterView.OnItemSelectedListener {

    private static final String LOG_TAG = "Jimm:StatusActivity";
    private XStatusAdapter xstatusAdapter;
    private Preferences p = JimmApplication.getInstance().getPreferences();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xtraz_dialog);
        setTitle(R.string.xstatus);

        final GridView gridXstatus = (GridView) findViewById(R.id.xstatus_grid);

        xstatusAdapter = new XStatusAdapter(getApplicationContext());

        gridXstatus.setAdapter(xstatusAdapter);
        gridXstatus.setOnItemSelectedListener(this);
        gridXstatus.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {

                Log.i(LOG_TAG, "onItemClick:position = " + position);

                final AccountsManager accountManager = JimmApplication.getInstance().getAccountsManager();

                if (position == 0) {
                    accountManager.setXStatus(0, null, null);
                    return;
                }

                final Dialog dialogXStatusText = new Dialog(XStatusActivity.this);
                dialogXStatusText.setContentView(R.layout.xtraz_text);
                dialogXStatusText.setTitle(R.string.xstatus);

                final EditText editTitle = (EditText) dialogXStatusText.findViewById(R.id.xstatus_title_edit);
                final EditText editDesciption = (EditText) dialogXStatusText.findViewById(R.id.xstatus_description_edit);

                editTitle.setText(p.xstatusTitles[position]);
                editDesciption.setText(p.xstatusDescriptions[position]);

                Button buttonSave = (Button) dialogXStatusText.findViewById(R.id.xstatus_save_button);
                buttonSave.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        accountManager.setXStatus(position, editTitle.getText().toString(), editDesciption.getText().toString());

                        dialogXStatusText.dismiss();
                        finish();
                    }

                });
                dialogXStatusText.show();
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        Log.i(LOG_TAG, "onItemSelected:position = " + position);
        p.xstatusIndex = position;
        p.save();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
        Log.i(LOG_TAG, "onNothingSelected");
    }
}